package com.ykdl.app.Utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

public class MyDateUtils {

	/**
	 * 获取今天星期几
	 * 
	 * @return
	 */
	public static String getTodayWeek() {
		return getWeek(null);
	}

	/**
	 * 获取星期几，传入日期,传入空为今天的星期几
	 */
	public static String getWeek(String pTime) {
		String Week = "星期";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 也可将此值当参数传进来
		Date curDate = new Date(System.currentTimeMillis());
		Calendar c = Calendar.getInstance();
		if (pTime == null) {

			String date = format.format(curDate);

			try {
				c.setTime(format.parse(date));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			try {
				c.setTime(format.parse(pTime));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		switch (c.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			Week += "天";
			break;
		case 2:
			Week += "一";
			break;
		case 3:
			Week += "二";
			break;
		case 4:
			Week += "三";
			break;
		case 5:
			Week += "四";
			break;
		case 6:
			Week += "五";
			break;
		case 7:
			Week += "六";
			break;
		default:
			break;
		}
		return Week;
	}

	/**
	 * 获取今天的日期 格式为 yyyy年MM月dd日 HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrentTime() {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
		String str = sdf.format(new Date());
		return str;
	}

	/**
	 * 获取昨天和明天的日期
	 * 
	 * @param num
	 *            1 为往后推一天 -1为往前推一天
	 * @return 格式为 yyyy-MM-dd
	 */
	public static String getDate(int num) {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, num);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(date);

		return dateString;

	}

	/**
	 * 获取今天的日期 格式为yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getTodayTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(date);
		System.out.println("今天的日期："+str);
		return str;
	}

	/**
	 * 传入日期，加一天，
	 */
	public static String getAddDate(String date) {
		//date = "2014-4-15";
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String data = null;
		try {
			Date dd = df.parse(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dd);
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			data = df.format(calendar.getTime());
		} catch (Exception e) {
			data = date;
		}
		return data;
	}

	/**
	 * 传入日期，减一天
	 */
	public static String getReduceDate(String str) {
		//date = "2014-4-15";
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String data = null;
		try {
			long dif = df.parse(str).getTime()-86400*1000;//减一天
			       Date date=new Date(); 
			       date.setTime(dif);
			       data = df.format(date);
			} catch (ParseException e) {
			e.printStackTrace();
			}
        return data;
	}
	/**
	 * 把格式为2014-04-04转为2014-4-4
	 * @return
	 */
		public static String getFitData(String oldData){
			String [] arr = oldData.split("-");
			String mYears = arr[0];
			String mMonths = arr[1];
			String mDays = arr[2];
			if (mMonths.contains("0")) {
				if (!mMonths.equals("10")) {
					
					mMonths = mMonths.substring(1);
				}
			}
			if (mDays.contains("0")) {
				if (!mDays.equals("10")&&!mDays.equals("20")&&!mDays.equals("30")) {
					
					mDays = mDays.substring(1);
				}
			}
			StringBuffer sb = new StringBuffer();
			sb.append(mYears).append("-").append(mMonths).append("-").append(mDays).toString();
		
			return sb.toString();
		}
		/**
		 * 把格式2014-4-4--->2014-04-04
		 */
		public static String getReversalData(String oldData){
			String [] arr = oldData.split("-");
			int mYears = Integer.parseInt(arr[0]);
			int mMonths = Integer.parseInt(arr[1]);
			int mDays = Integer.parseInt(arr[2]);
			StringBuffer sb = new StringBuffer();
			
			if (mMonths < 10) {
				if (mDays < 10) {

					sb.append(mYears).append("-").append(0).append(mMonths)
							.append("-").append(0).append(mDays);
				} else {
					sb.append(mYears).append("-").append(0).append(mMonths)
							.append("-").append(mDays);

				}

			} else {
				if (mDays < 10) {
					sb.append(mYears).append("-").append(mMonths).append("-")
							.append(0).append(mDays);
				} else {
					sb.append(mYears).append("-").append(mMonths).append("-")
							.append(mDays);
				}
			}
		
			return sb.toString();
			
		}
		/**
		 * 判断是否是过去的时间，是则返回true,
		 * @param mDateTime  传入当前显示的时间以判断
		 * @return
		 */
		public static boolean isPartingTime(String mDateTime){
			boolean flag = false;
			Long mToday = getLong(getTodayTime());
			Long mTime  = getLong(mDateTime);
			System.out.println("mDateTime="+mTime+"mToday"+mToday);
			if ((mTime-mToday)<0) {
				flag = true;
			}else {
				flag = false;
			}
			return flag;
		}
		private static  long getLong(String mDateTime){
			// mDateTime = getFitData(mDateTime);
			 String [] date = mDateTime.split("-");
			 StringBuffer sb = new StringBuffer();
			 for (int i = 0; i < date.length; i++) {
				sb.append(date[i]);
			 }
			 return Long.parseLong(sb.toString());
			
		}
		/**
		 * 获取当前时间
		 * @return
		 */
		public static String getTime() {
			SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm");
			Date localDate = new Date();
			String signtime_parameter = String.valueOf(localSimpleDateFormat
					.format(localDate));
			return signtime_parameter;
		}	
		/**
		 * 获取当前时间，没有年月日
		 */
		public static String getTimes(){
			SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
					" HH:mm:ss");
			Date localDate = new Date();
			String signtime_parameter = String.valueOf(localSimpleDateFormat
					.format(localDate));
			return signtime_parameter;
		}
		/**
		 * 获取六天前的日期
		 */
		
		public static String getReduce6Date() {
			String str = 	getTodayTime();
			//date = "2014-4-15";
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String data = null;
			try {
				long dif = df.parse(str).getTime()-86400*1000*6;//减一天
				       Date date=new Date(); 
				       date.setTime(dif);
				       data = df.format(date);
				} catch (ParseException e) {
				e.printStackTrace();
				}
	        return data;
		}
		/**
		 * 获取n天前的日期
		 */
		public static String getReduceNDate(int num){
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");  //字符串转换
			  Calendar c = Calendar.getInstance();  
			//new Date().getTime();这个是获得当前电脑的时间，你也可以换成一个随意的时间
			  c.setTimeInMillis(new Date().getTime());
			  c.add(Calendar.DATE, num);//天后的日期
			  Date date= new Date(c.getTimeInMillis()); //将c转换成Date
			//  System.out.println("date="+formatDate.format(date));
			
			return formatDate.format(date);
		}
		/*
		 * 获取文件名称
		 */
		public static String getFileName(String userID) {
			SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd-HH-mm");
			Date localDate = new Date();
			String	str_time = String.valueOf(localSimpleDateFormat.format(localDate));
			// return str ;
			return userID + "-" + str_time ;
		}
	
}
